﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.ToolPositioning.CApTPCompatibilityChecker
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System;


namespace DnProcessSimulateCommands.ToolPositioning;

internal class CApTPCompatibilityChecker
{
  public bool MatchType(
    string toolType,
    string fastenerType,
    string toolContourType,
    string fastenerContourType)
  {
    if (this.isOuter(toolType) && this.isOuter(fastenerType) || this.isInner(toolType) && this.isInner(fastenerType))
      return false;
    if (!(toolContourType != string.Empty) || !(fastenerContourType != string.Empty))
      return true;
    string[] strArray1 = toolContourType.Split(',');
    string[] strArray2 = fastenerContourType.Split(',');
    return strArray1 != null && strArray1.Length != 0 && strArray2 != null && strArray2.Length != 0 && strArray1[0] == strArray2[0];
  }

  public bool isOuter(string endEffectorType)
  {
    switch (endEffectorType)
    {
      case "Bit":
        return true;
      case "Bolt":
        return true;
      case "FlatScrew":
        return false;
      case "FlatScrewDriver":
        return true;
      case "Headed":
        return true;
      case "Hexagon":
        return false;
      case "HexagonDriver":
        return true;
      case "HexagonSocket":
        return false;
      case "HexagonSocketDriver":
        return true;
      case "Nut":
        return true;
      case "NutSetter":
        return false;
      case "Phillips":
        return true;
      case "PhillipsHead":
        return false;
      case "PhillipsSlottedScrew":
        return false;
      case "Pozidriv":
        return true;
      case "PozidrivHead":
        return false;
      case "Screw":
        return false;
      case "Socket":
        return false;
      case "SquareBit":
        return true;
      case "SquareBitDriver":
        return true;
      case "SquareSocket":
        return false;
      case "SquareSocketDriver":
        return true;
      case "Torx":
        return true;
      case "TorxHead":
        return false;
      case "UnknownFastener":
        return true;
      case "UnknownTool":
        return true;
      default:
        throw new InvalidOperationException("Unrecognized type");
    }
  }

  public bool isInner(string endEffectorType) => !this.isOuter(endEffectorType);

  private double GetTolerance(double val, int tolerancePercent)
  {
    return (double) tolerancePercent * val / 100.0;
  }

  public string LogTolerance(double toolRadius, double fastenerRadius, int toleranceSize)
  {
    return this.GetTolerance(Math.Max(toolRadius, fastenerRadius), toleranceSize).ToString("0.00");
  }

  public bool MatchSize(double toolRadius, double fastenerRadius, int toleranceSize)
  {
    double tolerance = this.GetTolerance(Math.Max(toolRadius, fastenerRadius), toleranceSize);
    return Math.Abs(toolRadius - fastenerRadius) <= tolerance;
  }
}
